<?php
/**
 * 权益内容
 * User: Acer
 * Date: 2022/7/17
 * Time: 14:31
 */
namespace app\admin\controller;

use app\admin\service\EquityService;
use app\admin\service\KdniaoService;
use app\core\MyController as MyController;

class Equity extends MyController
{
    protected $noNeedAction = ['export_equity_info_audit_list','express_query_job']; // 不需要校验控制器，通道
    // 操作前置
    protected $beforeActionList = ['my_initialize'];

    // 操作前置方法
    public function my_initialize()
    {
//        $this->is_admin();  // 权限检查
    }

    // 获取权益列表
    public function get_list(){
        $data = input();
        // 页数
        if(!isset($data['page']))  json_err( -201);
        // 当前页数
        if(!isset($data['page_number'])) $data['page_number'] = 10;
        // 查询
        $like =[];
        // 权益名称
        if( isset($data['title']) && $data['title'] ) $like['title'] = $data['title'];
        $res = (new EquityService())->s_get_list($data['page'],$data['page_number'],$like);
        if($res) _action_result(200,$res,'操作成功！');
        json_err( -205);
    }

    // 创建权益
    public function add_equity(){
        $data = input();
        if(empty($data['title'])  || empty($data['frequency']) )  json_err( -201);
        $res = (new EquityService())->s_add_equity($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 修改权益状态
    public function state_equity(){
        $data = input();
        if(empty($data['eid']))  json_err( -201);
        $res = (new EquityService())->s_state_equity($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 修改权益信息
    public function modify_equity(){
        $data = input();
        if(empty($data['eid']) || empty($data['title'])  || empty($data['frequency']) )  json_err( -201);
        (new EquityService())->s_modify_equity($data);
        _action_result(200,[],'操作成功！');
    }
    // 权益下拉列表
    public function get_select_list(){
        $res = (new EquityService())->s_get_select_list();
        _action_result(200,$res,'操作成功！');
    }
    // 用户使用权益并填写资料列表
    public function get_equity_info_list(){
        $data = input();
        if(!isset($data['page']))  json_err( -201);
        if(!isset($data['page_number'])) $data['page_number'] = 10;
        if((!isset($data['ct_id']))||(empty($data['ct_id']))) $data['ct_id'] = '8'; // TCR卡
        $res = (new EquityService())->get_equity_info_list($data['page'],$data['page_number'],$data['ct_id']);
        _action_result(200,$res,'操作成功！');
    }
    // 获取审核列表
    public function get_equity_info_audit_list(){
        $data = input();
        if(!isset($data['page']))  json_err( -201);
        if(!isset($data['page_number'])) $data['page_number'] = 10;
        if((!isset($data['ct_id']))||(empty($data['ct_id']))) $data['ct_id'] = '8'; // TCR卡
        if(!isset($data['name'])) $data['name']='';
        if(!isset($data['state'])) $data['state']='';
        if(!isset($data['logistic_code'])) $data['logistic_code']='';
        if(!isset($data['product_code'])) $data['product_code']='';
        $res = (new EquityService())->get_equity_info_audit_list($data['page'],$data['page_number'],$data['ct_id'],$data['name'],$data['state'],$data['logistic_code'],$data['product_code']);
        _action_result(200,$res,'操作成功！');
    }
    // 导出权益服务审核列表
    public function export_equity_info_audit_list(){
        $data = input();
        $list = (new EquityService())->export_equity_info_audit_list($data);
        $head = ['申请身份','激活权益人','主权益人','申请权益','审核状态','证件号码','年龄','性别','医院名称','入住科室','详细地址','目前身体既往病','服务日期','所属城市','紧急联系人','联系电话'];
        exportExcel($list, '金牌护理卡申请列表-'.date('Y-m-d'), $head, 10);
        unset($list);
        exit();
    }
    // 审核权益服务
    public function audit_equity_info(){
        $data = input();
        if(!isset($data['ue_id'])||!isset($data['state']))  json_err( -201);
        $res = (new EquityService())->audit_equity_info($data);
        _action_result(200,[],'操作成功！');
    }
    // 权益服务添加物流信息
    public function add_express_info(){
        $data = input();
        if(!isset($data['ue_id'])||!isset($data['logistic_code']))  json_err( -201);
        $res = (new EquityService())->add_express_info($data);
        _action_result(200,[],'操作成功！');
    }
    // 获取快递信息
    public function express_query(){
        $data = input();
        if(!isset($data['LogisticCode'])||empty($data['LogisticCode']))  json_err( -201);
        $res = (new KdniaoService())->express_query($data['LogisticCode']);
        _action_result(200,[$res],'操作成功！');
    }
    // 定时获取快递信息任务
    public function express_query_job(){
        (new KdniaoService())->express_query_job();
        _action_result(200,[],'操作成功！');
    }
}
